package com.common.base.adapter

import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import androidx.recyclerview.widget.RecyclerView
import com.bumptech.glide.Glide
import com.bumptech.glide.load.engine.DiskCacheStrategy
import com.common.base.R
import com.common.base.app.extras.load
import com.common.base.app.extras.no
import com.common.base.listener.OnItemLongClickListener
import com.common.base.utils.ImgUrlUtils
import com.common.base.utils.ViewUtils

/**
 *
 * 作者：sosou
 *
 * 版本：1.0
 *
 * 创建日期：7/2/2023
 *
 * 描述：
 *
 * 修订历史：
 *
 */
class CustomGridImageAdapter(context: Context?, result: List<String>?, roundSize: Float = 1f) :
    RecyclerView.Adapter<CustomGridImageAdapter.ViewHolder?>() {
    private val mInflater: LayoutInflater
    private var list = ArrayList<String>()
    private var selectMax = 9
    private var mRound = roundSize

    /**
     * 删除
     */
    fun delete(position: Int) {
        try {
            if (position != RecyclerView.NO_POSITION && list.size > position) {
                list.removeAt(position)
                notifyItemRemoved(position)
                notifyItemRangeChanged(position, list.size)
            }
        } catch (e: Exception) {
            e.printStackTrace()
        }
    }

    fun setSelectMax(selectMax: Int) {
        this.selectMax = selectMax
    }

    fun getSelectMax(): Int {
        return selectMax
    }

    fun getData(): List<String> {
        return list
    }

    fun remove(position: Int) {
        if (position < list.size) {
            list.removeAt(position)
        }
    }

    class ViewHolder(view: View) : RecyclerView.ViewHolder(view) {
        var mImg: ImageView

        init {
            mImg = view.findViewById(R.id.fiv)
        }
    }

    override fun getItemCount(): Int {
        return list.size
    }

    override fun getItemViewType(position: Int): Int {
        return position
    }

    /**
     * 创建ViewHolder
     */
    override fun onCreateViewHolder(viewGroup: ViewGroup, i: Int): ViewHolder {
        val view: View = mInflater.inflate(R.layout.base_custom_gv_filter_image, viewGroup, false)
        return ViewHolder(view)
    }

    /**
     * 设置值
     */
    override fun onBindViewHolder(viewHolder: ViewHolder, position: Int) {
        if (list.isEmpty()) {
            return
        }
        val media = list[position]
//        Glide.with(viewHolder.itemView.context)
//            .load(ImgUrlUtils.getOne2One(ImgUrlUtils.TYPE_HW, ImgUrlUtils.checkHasDomain(media)))
//            .centerCrop()
//            .placeholder(R.color.color_F6F6F6)
//            .diskCacheStrategy(DiskCacheStrategy.ALL)
//            .into(viewHolder.mImg)

        viewHolder.mImg.load(
            ImgUrlUtils.getOne2One(
                ImgUrlUtils.TYPE_HW,
                ImgUrlUtils.checkHasDomain(media)
            ), round = mRound
        )
        //itemView 的点击事件
        if (mItemClickListener != null) {
            viewHolder.itemView.setOnClickListener { v: View ->
                //防止多次点击
                if (ViewUtils.isFastClick()) return@setOnClickListener

                val adapterPosition = viewHolder.absoluteAdapterPosition
                mItemClickListener!!.onItemClick(v, adapterPosition)
            }
        }
    }

    private var mItemClickListener: OnItemClickListener? = null
    fun setOnItemClickListener(l: OnItemClickListener?) {
        mItemClickListener = l
    }

    interface OnItemClickListener {
        /**
         * Item click event
         *
         * @param v
         * @param position
         */
        fun onItemClick(v: View?, position: Int)

        /**
         * Open PictureSelector
         */
        fun openPicture()
    }

    private var mItemLongClickListener: OnItemLongClickListener? = null

    init {
        mInflater = LayoutInflater.from(context)
        result.isNullOrEmpty().no {
            list = result as ArrayList<String>
        }
    }

    fun setItemLongClickListener(l: OnItemLongClickListener?) {
        mItemLongClickListener = l
    }

    companion object {
        const val TAG = "CustomGridImageAdapter"
    }
}